domain=harbor.allen.com

rm -rf *.crt *.key *.cert *.csr *.ext *.srl

openssl genrsa -out ca.key 4096

openssl req -x509 -new -nodes -sha512 -days 3650 \
 -subj "/C=CN/ST=Beijing/L=Beijing/O=example/OU=Personal/CN=${domain}" \
 -key ca.key \
 -out ca.crt

openssl genrsa -out ${domain}.key 4096

openssl req -sha512 -new \
    -subj "/C=CN/ST=Beijing/L=Beijing/O=example/OU=Personal/CN=${domain}" \
    -key ${domain}.key \
    -out ${domain}.csr


cat > v3.ext <<-EOF
authorityKeyIdentifier=keyid,issuer
basicConstraints=CA:FALSE
keyUsage = digitalSignature, nonRepudiation, keyEncipherment, dataEncipherment
extendedKeyUsage = serverAuth
subjectAltName = @alt_names

[alt_names]
DNS.1=harbor.allen.com
DNS.2=harbor.allen
DNS.3=2centos
EOF

openssl x509 -req -sha512 -days 3650 \
    -extfile v3.ext \
    -CA ca.crt -CAkey ca.key -CAcreateserial \
    -in ${domain}.csr \
    -out ${domain}.crt

openssl x509 -inform PEM -in ${domain}.crt -out ${domain}.cert

# move cert
mkdir -p /data/cert
cp ${domain}.crt /data/cert/
cp ${domain}.key /data/cert/

mkdir -p /etc/docker/certs.d/${domain}/
rm -rf /etc/docker/certs.d/${domain}/*
cp ${domain}.cert /etc/docker/certs.d/${domain}/
cp ${domain}.key /etc/docker/certs.d/${domain}/
cp ca.crt /etc/docker/certs.d/${domain}/
